package org.example.seckilljava.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.example.seckilljava.entity.Quota;

@Mapper
public interface QuotaMapper extends BaseMapper<Quota> {
    @Select("select * from t_quota where goods_id = #{goodsID}")
    Quota getGoodsQuota(@Param("goodsID") long goodsID);

    @Insert("insert into t_quota(goods_id, num, killed_num) " +
            "values (#{globalQuota.goodsId}, #{globalQuota.num}, #{globalQuota.killedNum})")
    void save(@Param("globalQuota") Quota globalQuota);

    @Update("update t_quota set killed_num = killed_num + #{num} " +
            "where goods_id = #{goodsID} and killed_num + #{num} <= num")
    Integer incrKilledNum(@Param("goodsID") Long goodsID, @Param("num") Integer num);
}